package com.axend.aerosense.base.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.BluetoothLeScanner;
import android.content.Intent;

import com.blankj.utilcode.util.LogUtils;

@SuppressLint("MissingPermission")
public class BleUtils {

    private BluetoothAdapter bluetoothAdapter;

    private static class Holder{
        private static final BleUtils INSTANCE = new BleUtils();
    }

    public static BleUtils getInstance(){
        return Holder.INSTANCE;
    }

    private BleUtils(){
        init();
    }

    private void init() {
        //蓝牙适配器
        if (null == bluetoothAdapter){
            bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            if (null == bluetoothAdapter) {
                LogUtils.w("此设备没有蓝牙功能！");
            }
        }
    }


    public BluetoothAdapter getBluetoothAdapter() {
        return bluetoothAdapter;
    }

    /**
     * 蓝牙是否打开
     */
    public boolean isEnable() {
        if (null != bluetoothAdapter) {
            return bluetoothAdapter.isEnabled();
        }
        return false;
    }

    /**
     * 打开蓝牙
     */
    public void openBluetoothSettings(Activity activity) {
        if (null == bluetoothAdapter) {
            return;
        }
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        activity.startActivity(intent);
    }

    /**
     * 获取ble扫描器
     * @return
     */
    public BluetoothLeScanner getLeScanner(){
        return null == bluetoothAdapter ? null : bluetoothAdapter.getBluetoothLeScanner();
    }
}
